/**
 * Created by fanbo on 2017-07-20.
 */
var modelListTemplete = require('html!./model-list.html');
require('./modelList.less');
module.exports = {
  template: modelListTemplete,
  data:function(){
    return{

      sceneOptions:[],//场景下拉框
      industryOptions:[],//行业下拉框
      selected1:'',
      selected0:'',
      selectedSys1:'',
      selectedSys0:'',
      modelName:'',
      modelSysName:'',
      modelDataList:[],//我的模型
      systemModelDataList:[],//系统模型
      userId:localStorage.getItem('userId'),
      systemUserId:"admin",//系统用户
      dataCount:0,
      dataPageSize: 10,
      dataPageIndex: 1,
      modelPageSize: 10,
      modelPageIndex: 1
    }
  },
  created:function(){
    $('body').delegate('.model-type a','click',function(){
      var me = this;
      $(this).siblings().removeClass('active');
      $(this).addClass('active');
      $('.model-list').hide();
      $('#'+$(this).attr('attr-link')).show();
    });
    //初始化列表
    this.initModelList();
    this.initSystemModelList();
    //this.initDataPage("modelPage2");
  },
  mount:function () {


  },
  beforeMount:function(){
    this.industryList();
    this.sceneList();
  },
  updated:function(){
    //this.initCommonModel();
  },
  methods:{

    imgSuoClass:function(){
      //$(".img-suo").css('background','url(img/ic-thumbview-active.png) center center no-repeat');
      //$(".img-suo").siblings('.list-model').css('background','url(img/ic-listview-normal.png) center center no-repeat');
      $(".img-suo").parent().siblings('.model-content').eq(0).show().siblings('.model-content').hide();
      this.spanTableAll("table");
    },
    listModelClass:function(){
      //$(".list-model").css('background','url(img/ic-listview-active.png) center center no-repeat');
      //$(".list-model").siblings('.img-suo').css('background','url(img/ic-thumbview-normal.png) center center no-repeat');
      $(".list-model").parent().siblings('.model-content').eq(1).show().siblings('.model-content').hide();
      this.spanTableAll("table");
    },
    imgSuoClassSys:function(){
      //$(".img-suo-sys").css('background','url(img/ic-thumbview-active.png) center center no-repeat');
      //$(".img-suo-sys").siblings('.list-model-sys').css('background','url(img/ic-listview-normal.png) center center no-repeat');
      $(".img-suo-sys").parent().siblings('.model-content').eq(0).show().siblings('.model-content').hide();

    },
    listModelClassSys:function(){
      //$(".list-model-sys").css('background','url(img/ic-listview-active.png) center center no-repeat');
      //$(".list-model-sys").siblings('.img-suo-sys').css('background','url(img/ic-thumbview-normal.png) center center no-repeat');
      $(".list-model-sys").parent().siblings('.model-content').eq(1).show().siblings('.model-content').hide();
    },
    spanTableAll:function (tableA) {
      this.spanClassType(0);

      if(tableA=="table") {
        $("#allModelTable").show();
        $("#yfbModelTable").hide();
        $("#wfbModelTable").hide();
        $("#spanUlWfb").hide();
        $("#spanUlYfb").hide();
        $("#spanUlAll").show();
      }else if(tableA=="sysTable"){

      }
    },
    spanTableYfb:function (tableA) {
      this.spanClassType(1);
      if(tableA=="table"){
        $("#allModelTable").hide();
        $("#yfbModelTable").show();
        $("#wfbModelTable").hide();
        $("#spanUlAll").hide();
        $("#spanUlWfb").hide();
        $("#spanUlYfb").show();
      }else if(tableA=="sysTable"){

      }

    },
    spanTableWfb:function (tableA) {
      this.spanClassType(2);
      if(tableA=="table") {
        $("#allModelTable").hide();
        $("#yfbModelTable").hide();
        $("#wfbModelTable").show();
        $("#spanUlAll").hide();
        $("#spanUlYfb").hide();
        $("#spanUlWfb").show();
      }else if(tableA=="sysTable"){

      }
    },
    spanClassType:function (num) {
      if(num==0){
        $("span").siblings("#spanTableYfb").removeClass("active");
        $("span").siblings("#spanTableWfb").removeClass("active");
        $("span").siblings("#spanTableAll").removeClass("active");
        $("span").siblings("#spanTableAll").addClass("active");
      }else if(num==1){
        $("span").siblings("#spanTableAll").removeClass("active");
        $("span").siblings("#spanTableWfb").removeClass("active");
        $("span").siblings("#spanTableYfb").removeClass("active");
        $("span").siblings("#spanTableYfb").addClass("active");
      }else if(num==2){
        $("span").siblings("#spanTableAll").removeClass("active");
        $("span").siblings("#spanTableYfb").removeClass("active");
        $("span").siblings("#spanTableWfb").removeClass("active");
        $("span").siblings("#spanTableWfb").addClass("active");
      }else{
        $("span").siblings("#spanTableYfb").removeClass("active");
        $("span").siblings("#spanTableWfb").removeClass("active");
        $("span").siblings("#spanTableAll").removeClass("active");
        $("span").siblings("#spanTableAll").addClass("active");
      }

    },
    /**
     * 点击发布
     * @param mid
     */
    updateModelById:function (mid) {

      var me = this;
      this.$http.get('/eyeModelController/updateModelState.do?modelId='+mid+""+"&stateId=10003")
        .then(function(res){
          if(res.data.status === 'OK'){
            layer.msg("发布成功");
            this.initModelList();
          }else{
            layer.msg("发布失败");
          }
        }.bind(this));
    },
    deleteModelById:function (mid) {
      var msg = "您真的确定要删除吗？\n\n请确认！";
      if (confirm(msg)==true){
        var me = this;
        this.$http.get('/eyeModelController/deleteModelById.do?modelId='+mid+"")
          .then(function(res){
            if(res.data.status === 'OK'){
              layer.msg("删除成功");
              this.initModelList();
            }else{
              layer.msg("删除失败");
            }
          }.bind(this));
        return true;
      }else {
        return false;
      }
    },
    //行业下拉框
    industryList:function(){
      var me = this;
      this.$http.get('/eyeIndustryController/getIndustryList.do')
        .then(function(res) {
          if(res.data.status === 'OK'){
            me.industryOptions = res.data.result.list;
          }else{
            console.log('行业查询失败');
          }
        }.bind(this));
    },
    //场景下拉框
    sceneList:function(){
      var me = this;
      this.$http.get('/eyeSceneController/getSceneList.do')
        .then(function(res) {
          if(res.data.status === 'OK'){
            me.sceneOptions = res.data.result.list;
          }else{
            console.log('场景查询失败');
          }
        }.bind(this));
    },
    //列表初始化（我的模型）
    initModelList:function(){
      var me = this;
      this.$http.get('/eyeModelController/getModelList.do?loginId='+me.userId+'&userId='+
        me.userId+'&industryId='+me.selected0+'&sceneId='+me.selected1+'&modelName='+me.modelName+'&modelType='+""+"&modelState="+""+"&pageSize="+me.dataPageSize+'&pageIndex='+me.dataPageIndex)
          .then(function(res){
            if(res.data.status === 'OK'){
              me.modelDataList = res.data.result.list;
              me.dataCount = res.data.result.count;
            }else{
              console.log('');
            }
          }.bind(this));
    },
    //列表初始化（系统模型）
    initSystemModelList:function(){
      var me = this;
      this.$http.get('/eyeModelController/getModelList.do?loginId='+me.userId+'&userId='+
          me.systemUserId+'&industryId='+me.selectedSys0+'&sceneId='+me.selectedSys1+'&modelName='+me.modelSysName+'&modelType='+""+"&modelState='10003'")
        .then(function(res){
          if(res.data.status === 'OK'){
            me.systemModelDataList = res.data.result.list;
            me.dataCount =res.data.result.count;

          }else{
            console.log('');
          }
        }.bind(this));
    },
    //分页
    initDataPage:function (id){
      var me = this;
      laypage({
        cont: $('#'+id), //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
        pages: Math.ceil(me.dataCount/me.dataPageSize), //通过后台拿到的总页数
        //curr: totalNum || 1,
        curr: 1,
        skin: '#338af9',
        jump: function (obj, first) { //触发分页后的回调
          if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
            me.doChangeDataPage(obj.curr);
          }
        }
      });
    },
    doChangeDataPage:function(num){
      this.dataPageIndex = num;
      this.initModelList();
    },
  }
};
